package wili.attrMapping.file;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wili.attrMapping.core.AttsFile;
import wili.json.JsonFile0;
import wili.json.JsonLeaf;

/**
 * File which name ends with ".json"
 */
public class JsonFile extends JsonFile0 implements AttsFile {
	public JsonFile(File jsonFile) {
		super(jsonFile);
	}

	public JsonFile(String jsonSt) {
		super(null, jsonSt);
	}

	public static Map<String, List<AttsFile>> getAttsIdToFilesMap(String keyName, List<AttsFile> fromFiles) {
		if (fromFiles == null)
			return null;
		Map<String, List<AttsFile>> ret = new HashMap<>();
		for (AttsFile ele : fromFiles) {
			JsonFile0 file = (JsonFile) ele;
			List<JsonLeaf> lis = file.root.findLeafs(keyName, null, -1);
			for (JsonLeaf id : lis) {
				List<AttsFile> ff = ret.get(id.stringValue);
				if (ff == null) {
					ret.put(id.stringValue, ff = new ArrayList<>());
				}
				ff.add(ele);
			}
		}
		return ret;
	}
}
